/**
 * 
 */
package es.ibm.santillana.mdm.workflow.modificacion.especificos;


import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;

import com.ibm.pim.catalog.Catalog;
import com.ibm.pim.catalog.item.Item;
import com.ibm.pim.collaboration.CollaborationItem;
import com.ibm.pim.collaboration.CollaborationStep;
import com.ibm.pim.collaboration.ItemCollaborationArea;
import com.ibm.pim.collection.PIMCollection;
import com.ibm.pim.context.Context;
import com.ibm.pim.context.PIMContextFactory;
import com.ibm.pim.extensionpoints.WorkflowStepFunction;
import com.ibm.pim.extensionpoints.WorkflowStepFunctionArguments;
import com.ibm.pim.utils.Logger;

import es.ibm.santillana.mdm.common.bussinesmodel.ConfigModel;
import es.ibm.santillana.mdm.common.util.ConstSantillana;
import es.ibm.santillana.mdm.common.util.SantillanaLogger;
import es.ibm.santillana.mdm.integration.ChangesHandler;
import es.ibm.santillana.mdm.integration.ExportData;
import es.ibm.santillana.mdm.integration.ItemChanges;
import es.ibm.santillana.mdm.state.StateManager;

/**
 * @author Benito
 *
 */
public class Success implements WorkflowStepFunction {

	/**
	 * 
	 */
	public Success() {
		// TODO Auto-generated constructor stub
	}

	/* (non-Javadoc)
	 * @see com.ibm.pim.extensionpoints.WorkflowStepFunction#in(com.ibm.pim.extensionpoints.WorkflowStepFunctionArguments)
	 */
	@Override
	public void in(WorkflowStepFunctionArguments arg0) {}

	/* (non-Javadoc)
	 * @see com.ibm.pim.extensionpoints.WorkflowStepFunction#out(com.ibm.pim.extensionpoints.WorkflowStepFunctionArguments)
	 */
	@Override
	public void out(WorkflowStepFunctionArguments arg0) {
		Logger log = SantillanaLogger.getLogger();
		log.logInfo("OUT del Success:");
		System.out.println("OUT del Success:");
		
		Context ctx =  PIMContextFactory.getCurrentContext();
		
		PIMCollection <CollaborationItem> collabItem = arg0.getItems();
		
		Iterator <CollaborationItem> it = collabItem.iterator();
			
		while (it.hasNext()) {
			
			CollaborationItem collabitem = it.next();
			log.logInfo("Mando collabitem: " + collabitem.getPrimaryKey());
			ChangesHandler changesMod = new ChangesHandler(collabitem.getSourceItem(),collabitem,collabitem.getSourceItem().getCatalog().getName(), collabitem.getPrimaryKey(), ctx);
			changesMod.createChanges();
			try {
				ExportData exportData = new ExportData();
				exportData.exportXML(changesMod.getIntegrationChanges());
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}			
		}
//		boolean ok = colArea.interimCheckin(collabItem, step);
//		System.out.println("out del success: " + ok);
	}
	

	/* (non-Javadoc)
	 * @see com.ibm.pim.extensionpoints.WorkflowStepFunction#timeout(com.ibm.pim.extensionpoints.WorkflowStepFunctionArguments)
	 */
	@Override
	public void timeout(WorkflowStepFunctionArguments arg0) {
		// TODO Auto-generated method stub

	}

}
